package gui;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.JLabel;
import javax.swing.JPanel;

import mpc.Транслятор_Строки;
import mpc.Tree_Node;

public class OldTreeDrawingPanel extends JPanel{
	
	public static Font font = new Font("arial", Font.BOLD, 20);
	
	private static final int TOP_SPACE = 30, BOTTOM_SPACE = 20, LEFT_SPACE = 20, RIGHT_SPACE = 20;
	
	private boolean isNiceMode = true;
	
	private Tree_Node root;
	//private int width, height;
	
	private int[] coordToPaintLeftTop(Tree_Node node){
		int[] r = coordToPaintCenter(node);
			
		/*
			r[0] = node.rowLength() > 1 ? (this.getWidth() - node.fullWidthInRow() - LEFT_SPACE - RIGHT_SPACE) * node.placeInRow() 
					/ (node.rowLength() - 1) + node.widthBefore() + LEFT_SPACE : (this.getWidth() - node.labelWidth()) / 2;
			r[1] = node.numberOfLevels() > 1 ?
					(this.getHeight() - TOP_SPACE - BOTTOM_SPACE) * node.level() / (node.numberOfLevels() - 1)
					+ TOP_SPACE : (this.getHeight()) / 2;
			*/
		
			r[0] -= node.labelWidth() / 2;
			
		return r;
	}
	
	private int[] coordToPaintCenter(Tree_Node node){
		int[] r = new int[2];
		if(isNiceMode){
			//double pos = (node.centerPositionForNiceMode() - node.minNormalPos()) / (node.maxNormalPos() - node.minNormalPos());
			r[0] = (int) (LEFT_SPACE + 
					Math.round((this.getWidth() - LEFT_SPACE - RIGHT_SPACE) * node.centerPositionForNiceMode()));
		} else 	
			r[0] = node.rowLength() > 1 ? (this.getWidth() - node.fullWidthInRow() - LEFT_SPACE - RIGHT_SPACE) * node.placeInRow() 
					/ (node.rowLength() - 1) + node.widthBefore() + LEFT_SPACE : (this.getWidth() - node.labelWidth()) / 2;
			
		r[1] = node.numberOfLevels() > 1 ?
				(this.getHeight() - TOP_SPACE - BOTTOM_SPACE) * node.level() / (node.numberOfLevels() - 1)
				+ TOP_SPACE : (this.getHeight()) / 2;;
		return r;
	}
	
	private void paintParentLine(Tree_Node node, Graphics g){
		
		if(node.getParent() == null) return;
		
		int[] nodeCenter = coordToPaintCenter(node), parentCenter = coordToPaintCenter(node.getParent());
		
		g.setColor(Color.black);
		//g.se
		g.drawLine(nodeCenter[0], nodeCenter[1], parentCenter[0], parentCenter[1]);
	}
	
	private void paintAllLines(Tree_Node node, Graphics g){
		if(node == null) return;
		
		paintParentLine(node, g);
		
		paintAllLines(node.leftSone(), g);
		paintAllLines(node.rightSone(), g);
	}
	
	private void paintAllLabels(Tree_Node node, Graphics g){
		if (node == null) return;
		
		//Graphics g = this.getGraphics();
		
		int[] coords = coordToPaintLeftTop(node);
		
		g.setColor(Color.white);
		g.fillRect(coords[0] - 2, coords[1] - node.labelHeight() + 2, node.labelWidth() + 4, node.labelHeight() + 5);
		g.setColor(Color.DARK_GRAY);
		g.drawRect(coords[0] - 2, coords[1] - node.labelHeight() + 2, node.labelWidth() + 4, node.labelHeight() + 5);
		g.setColor(node.getColor());
		g.drawString(node.getLabel(), coords[0], coords[1]);

		
		
		paintAllLabels(node.leftSone(), g);
		paintAllLabels(node.rightSone(), g);
	}
	
	public void setNiceMode(boolean b){
		isNiceMode = b;
		this.attachTree(root);
	}
	
	public void attachTree(Tree_Node root){
		if(root == null) return;
		
		//System.out.println("Tree Min Height: " + (root.minTreePixelHeight() + TOP_SPACE + BOTTOM_SPACE));
		//MainFrame.WorkFrame.pack();
		//MainFrame.WorkFrame.setMinimumSize(MainFrame.WorkFrame.getSize());
		
		this.setFont(font);
		this.root = root;
		root.initSituation(this.getGraphics().getFontMetrics());
		
		int mw = (isNiceMode ? root.minTreePixelWidthForNiceMode()
				: root.minTreePixelWidthForEffectiveMode())
				+ LEFT_SPACE + RIGHT_SPACE + 100,
			mh = root.minTreePixelHeight() + TOP_SPACE + BOTTOM_SPACE;
		
		if(mw < Визуализатор_для_Скобок.minWidth)
			mw = Визуализатор_для_Скобок.minWidth;
		
		if(mh < Визуализатор_для_Скобок.minHeight)
			mh = Визуализатор_для_Скобок.minWidth;
		
		Визуализатор_для_Скобок.WorkFrame.setMinimumSize(new Dimension( mw,
				mh));
		//root.checkTree();
		this.getParent().repaint();
	}
	
	@Override
	public void paint(Graphics g){
		//g.setFont(font);
		//root.initSituation(g.getFontMetrics());
		//root = StringRebuilder.getTree("a 1 + b 2 + c 3");
		//root.checkTree();
		g.setFont(font);
		g.setColor(Color.white);
		g.fillRect(0, 0, this.getWidth(), this.getHeight());
		paintAllLines(root, g);
		paintAllLabels(root, g);
	}
	
}
